package com.hzmall.controller;

import com.hzmall.common.ResponseCode;
import com.hzmall.common.ServerResponse;
import com.hzmall.pojo.User;
import com.hzmall.service.impl.OrderService;
import com.hzmall.util.CheckLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 生成订单
     */
    @RequestMapping(value = "create.do")
    @ResponseBody
    public ServerResponse create(HttpServletRequest request,Integer shippingId){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.orderService.create(currentUser.getId(),shippingId);
    }

    /**
     * 获取订单的商品信息
     */
    @RequestMapping(value = "get_order_cart_product.do")
    @ResponseBody
    public ServerResponse getOrderCartProduct(HttpServletRequest request){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.orderService.getOrderCartProduct(currentUser.getId());
    }

    /**
     * 订单列表
     */
    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse list(HttpServletRequest request, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.orderService.list(currentUser.getId(), pageNum,pageSize);
    }

    /**
     * 订单详情
     */
    @RequestMapping(value = "detail.do")
    @ResponseBody
    public ServerResponse detail(HttpServletRequest request, Long orderNo){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.orderService.detail(currentUser.getId(), orderNo);
    }

    /**
     * 取消订单
     */
    @RequestMapping(value = "cancel.do")
    @ResponseBody
    public ServerResponse cancel(HttpServletRequest request, Long orderNo){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.orderService.cancel(currentUser.getId(), orderNo);
    }

    /**
     * 支付
     */
    @RequestMapping(value = "pay.do")
    @ResponseBody
    public ServerResponse pay(HttpServletRequest request, Long orderNo){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.orderService.pay(currentUser.getId(), orderNo);
    }

    /**
     * 处理支付宝回调
     */
    @RequestMapping(value = "alipay_callback.do")
    @ResponseBody
    public Object alipayCallback(HttpServletRequest request){
        Map<String, String[]> paramMap = new HashMap<String, String[]>();
        paramMap = request.getParameterMap();
        return this.orderService.alipayCallBack(paramMap);
    }

    /**
     * 查询订单支付状态
     */
    @RequestMapping(value = "query_order_pay_status.do")
    @ResponseBody
    public ServerResponse queryOrderPayStatus(HttpServletRequest request,Long orderNo){
        User currentUser = CheckLoginUtil.doCheckLogin(request);
        if (currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        ServerResponse response = this.orderService.queryOrderPayStatus(currentUser.getId(),orderNo);
        if (response.isSuccess()){
            return ServerResponse.createBySuccess(true);
        }
        return response;
    }
}
